/*
 * ImNativeC.c
 *
 *  Created on: 2013-5-30
 *      Author: Stan Yung
 */
#include <jni.h>
#include <string.h>
#include <android/log.h>

#define TAG "native-ladyservice"
#define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, TAG, __VA_ARGS__))
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__))
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__))
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__))

void Java_com_stanyung_nativestart_inter_LadyService_deleteLady(JNIEnv *env,
		jobject thiz, jlong ladyId) {
	LOGI("LadyService.deleteLady(%ld)", ladyId);
}

jboolean Java_com_stanyung_nativestart_inter_LadyService_updateLady(JNIEnv *env,
		jobject thiz, jlong ladyId, jobject lady) {
	LOGI("LadyService.updateLady(%ld, obj)", ladyId);
	jclass clazz = (*env)->FindClass(env, "com/stanyung/nativestart/model/Lady");
	jmethodID methodID = (*env)->GetStaticMethodID(env, clazz, "parse", "(Lcom/stanyung/nativestart/model/Lady;)Ljava/lang/String;");
	 jstring str = (*env)->CallStaticObjectMethod(env, clazz, methodID, lady);	//?
	const char *strC = (*env)->GetStringUTFChars(env, str, NULL);
	LOGD(str);
	(*env)->ReleaseStringUTFChars(env, str, strC);
	return 1;
}

jobject Java_com_stanyung_nativestart_inter_LadyService_addLady(JNIEnv *env,
		jobject thiz, jstring name, jint age, jobjectArray addrs,
		jdoubleArray distances) {
	const char *nameC = (*env)->GetStringUTFChars(env, name, NULL);
	LOGI("LadyService.addLady(%s, %d, array, array)", nameC, age);
	(*env)->ReleaseStringUTFChars(env, name, nameC);
	jclass clazz = (*env)->FindClass(env, "com/stanyung/nativestart/model/Lady");
	jmethodID methodID = (*env)->GetMethodID(env, clazz, "<init>", "(JLjava/lang/String;I)V");
	jlong ladyId = 1200;
	jobject lady = (*env)->NewObject(env, clazz, methodID, ladyId, name, age);
	return lady;
}

jobject createLady(JNIEnv *env, jclass clazz, long ladyId, char *name, int age) {
	jmethodID methodID = (*env)->GetMethodID(env, clazz, "<init>", "()V");
	jobject lady = (*env)->NewObject(env, clazz, methodID);
	methodID = (*env)->GetMethodID(env, clazz, "setLadyId", "(J)V");
	(*env)->CallVoidMethod(env, lady, methodID, ladyId);
	methodID = (*env)->GetMethodID(env, clazz, "setName", "(Ljava/lang/String;)V");
	jstring nameJ = (*env)->NewStringUTF(env, name);
	(*env)->CallVoidMethod(env, lady, methodID, nameJ);
	methodID = (*env)->GetMethodID(env, clazz, "setAge", "(I)V");
	(*env)->CallVoidMethod(env, lady, methodID, age);
	return lady;
}

jobjectArray Java_com_stanyung_nativestart_inter_LadyService_queryLadyArray(
		JNIEnv *env, jobject thiz, jint size) {
	LOGI("LadyService.queryLadyArray(%d)", size);
	jclass clazz = (*env)->FindClass(env, "com/stanyung/nativestart/model/Lady");
	jobjectArray array = (*env)->NewObjectArray(env, size, clazz, NULL);
	int i;
	for(i = 0; i < size; i ++ ) {
		long ladyId = 1200;
		char *name = "Nancy";
		int age = 18;
		jobject lady = createLady(env, clazz, ladyId, name, age);
		(*env)->SetObjectArrayElement(env, array, i, lady);
	}
	return array;
}

//jobject Java_com_stanyung_nativestart_inter_LadyService_queryLadyList(
//		JNIEnv *env, jobject thiz) {
//	LOGI("LadyService.queryLadyList()");
//	return NULL;
//}
